package com.github.pig.admin.service.tsjy.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.github.pig.admin.mapper.tsjy.SuggestRecordMapper;
import com.github.pig.admin.model.entity.SysDict;
import com.github.pig.admin.model.tsjy.SuggestRecord;
import com.github.pig.admin.service.tsjy.ISuggestRecordService;
import com.github.pig.admin.util.AjaxJson;
import com.github.pig.admin.util.CommonService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 投诉建议记录表 服务实现类
 * </p>
 *
 * @author lxm
 * @since 2018-06-22
 */
@Service
public class SuggestRecordServiceImpl extends ServiceImpl<SuggestRecordMapper, SuggestRecord> implements ISuggestRecordService {

    @Autowired
    private SuggestRecordMapper suggestRecordMapper;
    @Autowired
    private CommonService commonService;
    @Override
    public SuggestRecord getSuggestRecordById(String suggestRecordId) {
        return suggestRecordMapper.getSuggestRecordById(suggestRecordId);
    }

    /**
     * 投诉/建议评价接口
     * @param suggestRecord
     * @return
     */
    @Override
    public AjaxJson addEstimate(SuggestRecord suggestRecord) {
        AjaxJson ajaxJson = new AjaxJson();
        SuggestRecord suggestRecord1 = suggestRecordMapper.selectById(suggestRecord.getId());
        if (suggestRecord1 == null){
            ajaxJson.setMsg("操作有误，请稍后重试。");
            ajaxJson.setSuccess(false);
        }else {
            suggestRecord.setStatus("3");
            int res = suggestRecordMapper.updateById(suggestRecord);
            if (res == 0){
                ajaxJson.setMsg("操作有误，请稍后重试。");
                ajaxJson.setSuccess(false);
            }else {
                ajaxJson.setMsg("提交成功。");
                ajaxJson.setSuccess(true);
            }
        }
        return ajaxJson;
    }

    /**
     * 投诉/建议查询接口
     * @return
     */
    @Override
    public AjaxJson querySuggest(String id) {
        AjaxJson ajaxJson = null;
        try {
            ajaxJson = new AjaxJson();
            SuggestRecord suggestRecord = this.selectById(id);
            if (suggestRecord != null){
                List<SysDict> dictType = commonService.queryDicByType("suggest_type",suggestRecord.getSuggestType());
                //suggestRecord.setSuggestTypeName(dictType.get(0).getLabel());
                List<SysDict> dictStatus = commonService.queryDicByType("suggest_status",suggestRecord.getStatus());
                suggestRecord.setStatusName(dictStatus.get(0).getLabel());
            }
            ajaxJson.setMsg("查询成功。");
            ajaxJson.setObj(suggestRecord);
            ajaxJson.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            ajaxJson.setMsg("查询失败。");
            ajaxJson.setSuccess(false);
        }
        return ajaxJson;
    }

    /**
     * 分页查询投诉建议信息
     *
     * @return 分页对象
     */
    @Override
    public Page<SuggestRecord> suggestPage(Map<String, Object> params) {
        EntityWrapper<SuggestRecord> entityWrapper = new EntityWrapper<SuggestRecord>();
        Page<SuggestRecord> page = new Page<SuggestRecord>(Integer.parseInt(params.get("page").toString()),
                Integer.parseInt(params.get("limit").toString()));
        entityWrapper.eq("owner_id", params.get("ownerId"));
        entityWrapper.eq("del_flag", "0");
        entityWrapper.orderBy("suggest_time", false);
        Page<SuggestRecord> pageList = this.selectPage(page, entityWrapper);
        List<SuggestRecord> list = pageList.getRecords();
        if (list.size() > 0) {
            for (SuggestRecord suggestRecord : list) {
                if (StringUtils.isNotBlank(suggestRecord.getSuggestType())) {
                    if (suggestRecord.getSuggestType().equals("1")) {
                        //suggestRecord.setSuggestTypeName(CommonEnum.COMPLAIN.getName());
                    } else if (suggestRecord.getSuggestType().equals("2")) {
                        //suggestRecord.setSuggestTypeName(CommonEnum.SUGGEST.getName());
                    }
                }
                if (StringUtils.isNotBlank(suggestRecord.getDescription())){
                    if (suggestRecord.getDescription().length() > 15){
                        String head = suggestRecord.getDescription().substring(0,15);
                        suggestRecord.setDescription(head+"。。。");
                    }
                }
            }
        }
        return pageList;
    }
}
